﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VPC.BO;
using VPC.BL;

namespace VPC.UI
{
    public partial class EditContact : Form
    {
        public EditContact()
        {
            InitializeComponent();
        }

        //Fetching an Existing Contact Details in Text Boxes
        public void EditContact_Load()
        {
            Int32 iContactId = Convert.ToInt32(labelContactId.Text);
            ContactBO objContactBO = new ContactBO();

            objContactBO = ContactBL.GetContactDetails(iContactId);

            firstNameTextfield.Text         = objContactBO.FirstName;
            lastNameTextfield.Text          = objContactBO.LastName;
            fonctionTextfield.Text          = objContactBO.Fonction;
            compagnyTextfield.Text          = objContactBO.Compagny;
            mobileNumber1Textfield.Text     = objContactBO.MobileNumber1;
            mobileNumber2Textfield.Text     = objContactBO.MobileNumber2;
            mobileNumber3Textfield.Text     = objContactBO.MobileNumber3;
            homeNumber1Textfield.Text       = objContactBO.HomeNumber1;
            homeNumber2Textfield.Text       = objContactBO.HomeNumber2;
            homeNumber3Textfield.Text       = objContactBO.HomeNumber3;
            workNumber1Textfield.Text       = objContactBO.WorkNumber1;
            workNumber2Textfield.Text       = objContactBO.WorkNumber2;
            workNumber3Textfield.Text       = objContactBO.WorkNumber3;
            faxNumber1Textfield.Text        = objContactBO.FaxNumber1;
            faxNumber2Textfield.Text        = objContactBO.FaxNumber2;
            faxNumber3Textfield.Text        = objContactBO.FaxNumber3;
            streetTextfield.Text            = objContactBO.Street;
            localityTextfield.Text          = objContactBO.Locality;
            codeTextfield.Text              = objContactBO.Code;
            countryTextfield.Text           = objContactBO.Country;
            email1Textfield.Text            = objContactBO.Email1;
            email2Textfield.Text            = objContactBO.Email2;
            email3Textfield.Text            = objContactBO.Email3; 

        }

        //Updating the Contact in DB
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            ContactBO objContactBO = new ContactBO();

            objContactBO.ContactId = Convert.ToInt32(labelContactId.Text);
            objContactBO.FirstName = firstNameTextfield.Text;
            objContactBO.LastName = lastNameTextfield.Text;
            objContactBO.Fonction = fonctionTextfield.Text;
            objContactBO.Compagny = compagnyTextfield.Text;
            objContactBO.MobileNumber1 = mobileNumber1Textfield.Text;
            objContactBO.MobileNumber2 = mobileNumber2Textfield.Text;
            objContactBO.MobileNumber3 = mobileNumber3Textfield.Text;
            objContactBO.HomeNumber1 = homeNumber1Textfield.Text;
            objContactBO.HomeNumber2 = homeNumber2Textfield.Text;
            objContactBO.HomeNumber3 = homeNumber3Textfield.Text;
            objContactBO.WorkNumber1 = workNumber1Textfield.Text;
            objContactBO.WorkNumber2 = workNumber2Textfield.Text;
            objContactBO.WorkNumber3 = workNumber3Textfield.Text;
            objContactBO.FaxNumber1 = faxNumber1Textfield.Text;
            objContactBO.FaxNumber2 = faxNumber2Textfield.Text;
            objContactBO.FaxNumber3 = faxNumber3Textfield.Text;
            objContactBO.Street = streetTextfield.Text;
            objContactBO.Locality = localityTextfield.Text;
            objContactBO.Code = codeTextfield.Text;
            objContactBO.Country = countryTextfield.Text;
            objContactBO.Email1 = email1Textfield.Text;
            objContactBO.Email2 = email2Textfield.Text;
            objContactBO.Email3 = email3Textfield.Text;

            int result = ContactBL.UpdateContactDetails(objContactBO);

            if (result > 0)
            {
                MessageBox.Show("Contact Updated Successfully");
                this.Hide();
            }
            else
                MessageBox.Show("Failed to update Contact");

        }
    }
}
